<?php
/**
 * 房源类
 *
 * @version 	0.0.1
 */

class organizationmodel extends Model 
{
	
	// ------------------------------------------------------------------------
	/**
	 * initialises the class inheriting the methods of the class Model 
	 *
	 * @return Systemparam
	 */
    function organizationmodel()
    {
        parent::Model();
    }
	
    // ------------------------------------------------------------------------
    
    /**
     * 搜索房源信息，参数为条件数组
     * 
     * @return 记录集
     */
    function search($condition, $count = false)
    {
	    if ($count==true)
	    {
	    	$this->db->select('count(tb_agency.id) as count');
	    }
	    else
	    {
	    	$this->db->select('tb_agency.*');
			//$this->db->select('count(tb_agency.id) as count');
	    }
		//$this->db->join('district', 'tb_agency.FK_districtID = district.id');
/*	    if (!empty($condition['id']))
	    {
	    	$this->db->where('tb_agency.id', $condition['id']);
	    }	
		// 租售 类型
	    if (!empty($condition['searchType']))
	    {
	    	$this->db->where('type', $condition['searchType']);
	    }
	    // 景观
	    if (!empty($condition['sight']))
	    {
	    	$this->db->like('DIS_SIGN', '-'.$condition['sight'].'-');
	    }
	    // 用途
	    if (!empty($condition['purpose']))
	    {
	    	$this->db->where('PURPOSE', $condition['purpose']);
	    }
	    // 房
	    if (!empty($condition['room']))
	    {
	    	$this->db->where('house_fang', $condition['room']);
	    }
	    // 厅
	    if (!empty($condition['hall']))
	    {
	    	$this->db->where('house_ting', $condition['hall']);
	    }
	    // 价格1
	    if (!empty($condition['money1']))
	    {
	    	$this->db->where('sale_price >=', $condition['money1']);
	    }
	    // 价格2
	    if (!empty($condition['money2']))
	    {
	    	$this->db->where('sale_price <=', $condition['money2']);
	    }
	    // 业主
	    if (!empty($condition['owner']))
	    {
			$this->db->like('HOUSE_OWNER', $condition['owner']);
	    }
*/
	    // 分页显示
	    if ($count==false && isset($condition['start']) && isset($condition['limit']))
	    {
			$this->db->limit($condition['limit'], $condition['start']);
	    }

	    $query = $this->db->get('agency');

        if ($query->num_rows() > 0)
        {          		 
			return $query->result();
        }
    	return ;
    }
    
    // 根据ID获取房源记录，返回记录集
    function getHouseById($id)
    {
        //$this->db->select('*');
        $this->db->where('id', $id);
        $query = $this->db->get('agency');
        
        if ($query->num_rows() > 0)
        {
            return $query->row();
        }
    	return ;
    }

	// 新增房源
	function add($param)
	{
	    $obj =& get_instance();
	    $obj->load->model('Districtmodel');
        
        $row = $obj->Districtmodel->getDistrictById($param['district1']);
		$district1 = $row->NAME;

		$district2id = $obj->Districtmodel->getDistrictIdByName($param['district1'], $param['district2']);
		$district3id = $obj->Districtmodel->getDistrictIdByName($district2id, $param['district3']);
//print_r($param);
		foreach ($param as $key => $value)
		{
			if ($value!='')
			{
				$data[$key] = $value;
			}
		}
//		$data = array(
//					'type' =>  $param['type'],
//					'status' => $param['status'],
//					'CONSIGN_TYPE' => $param['CONSIGN_TYPE'],
//					'FK_districtID' => $param['district1'],
//					'district1' => $district1,
//					'district2' => $param['district2'],
//					'district3' => $param['district3'],
//					'doorplate' => $param['doorplate'],
//					'location' => $param['location'],
//					'FLOOR' => $param['FLOOR'],
//					'TOP_FLOOR' => $param['TOP_FLOOR'],
//					'HOUSE_AGE' => $param['HOUSE_AGE'],
//					'PURPOSE' => $param['PURPOSE'],
//					'house_fang' => $param['house_fang'],
//					'house_ting' => $param['house_ting'],
//					'house_wei' => $param['house_wei'],
//					'house_tai' => $param['house_tai'],
//					'build_size' => $param['build_size'],
//					'use_size' => $param['use_size'],
//					'place_size' => $param['place_size'],
//					'car_size' => $param['car_size'],
//					'garden_size' => $param['garden_size'],
//					'hovel_size' => $param['hovel_size'],
//					'DIRECTION' => $param['DIRECTION'],
//					'SOURCE' => $param['SOURCE'],
//					'fitment' => $param['fitment'],
//					'jiaju' => $param['jiaju'],
//					'jiadian' => $param['jiadian'],
//					'fuyong' => $param['fuyong'],
//					'property' => $param['property'],
//					'zhengjian' => $param['zhengjian'],
//					'kit_info' => $param['kit_info'],
//					'kit_memo' => $param['kit_memo'],
//					'zhuanrang_price' => $param['zhuanrang_price'],
//					'pay_type' => $param['pay_type'],
//					'money_type' => $param['money_type'],
//					'manager_fee' => $param['manager_fee'],
//					'manager_unit' => $param['manager_unit'],
//					'HOUSE_OWNER' => $param['HOUSE_OWNER'],
//					'HOUSE_OWNER_tel' => $param['HOUSE_OWNER_tel'],
//					'linkman' => $param['linkman'],
//					'linkman_tel' => $param['linkman_tel'] ,
//					'HOUSE_OWNER_memo' => $param['HOUSE_OWNER_memo'],
//					'input_date' => date('Y-m-d H:i:s'),
//					'input_userid' => $param['duty_userid'],
//					//'input_deptid' => $param['input_deptid'],
//					'contract_date' => $param['contract_date'],
//					//'feedback_date' => $param['feedback_date'],
//					'duty_userid' => $param['duty_userid'],
//
//					//'gj_date' => $param['gj_date'],
//					//'house_image' => $param['house_image'],
//					//'if_sc' => $param['if_sc'],
//					//'sc_date' => $param['sc_date']
//
////					'if_tuijian' => $param['if_tuijian'],
////					'if_encrypt_owner' => $param['if_encrypt_owner'],
////					'if_encrypt_buildsrc' => $param['if_encrypt_buildsrc'],
////					'if_encrypt_dept' => $param['if_encrypt_dept']
////
////					'if_KEY' => $param['if_KEY'],
////					'if_elevator' => $param['if_elevator'],
////					'if_attic' => $param['if_attic'],
//		           );

		$data['FK_districtID'] = $district3id;
		$data['district1'] = $district1;
		$data['input_date'] = date('Y-m-d H:i:s');
		$data['input_userid'] = $param['duty_userid'];

		if (array_key_exists('key_type',$param))
		{
			// 移除非数据库字段的Key，这一步一定要注意
			unset($data['key_type']);
			unset($data['key_num']);

			if ($param['key_type']=='0')
			{
				$data['publickey_num'] = $param['key_num'];
			}
			else
			{
				$data['privatekey_num'] = $param['key_num'];
			}
		}
		else
		{
			//$data['publickey_num'] = $param['key_num'];
			//$data['privatekey_num'] = $param['key_num'];
		}

		$this->db->insert('tb_agency', $data);

		return true;
	}

	// 编辑房源
	function edit($param)
	{
	    $obj =& get_instance();
	    $obj->load->model('Districtmodel');

        $row = $obj->Districtmodel->getDistrictById($param['district1']);
		$district1 = $row->NAME;

		$district2id = $obj->Districtmodel->getDistrictIdByName($param['district1'], $param['district2']);
		$district3id = $obj->Districtmodel->getDistrictIdByName($district2id, $param['district3']);

		$data['if_elevator'] = 0;
		$data['if_attic'] = 0;
		$data['if_KEY'] = 0;
		$data['if_tuijian'] = 0;
		$data['if_encrypt_owner'] = 0;
		$data['if_encrypt_buildsrc'] = 0;
		$data['if_encrypt_dept'] = 0;

		foreach ($param as $key => $value)
		{
			if ($value!='')
			{
				$data[$key] = $value;
			}
		}

		// 编辑时，code即为id
		$id = $param['code'];
		$data['FK_districtID'] = $district3id;
		$data['district1'] = $district1;
		$data['input_date'] = date('Y-m-d H:i:s');
		$data['input_userid'] = $param['duty_userid'];

		if (array_key_exists('key_type',$param))
		{

			if ($param['key_type']=='0')
			{
				$data['publickey_num'] = $param['key_num'];
			}
			else
			{
				$data['privatekey_num'] = $param['key_num'];
			}
		}

		// 所有需要的变量设置完毕
		// 移除非数据库字段的Key，这一步一定要注意
		unset($data['key_type']);
		unset($data['key_num']);
		// 不需要更新字段id，所以也一并移除
		unset($data['id']);

		$this->db->where('id', $id);
		$this->db->update('tb_agency', $data); 

		return true;
	}

	// 删除房源
	function delete($param)
	{
		foreach ($param as $value)
		{
			if (is_numeric($value))
			{
				$this->db->orwhere('id', $value);
			}
		}
		$this->db->delete('tb_agency');

		return true;
	}
}